package com.ruoyi.application.service.impl;

import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.application.domain.Application;
import com.ruoyi.application.domain.ResultVo;
import com.ruoyi.application.mapper.ApplicationMapper;
import com.ruoyi.application.service.IApplicationService;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.message.domain.Message;
import com.ruoyi.message.service.IMessageService;
import com.ruoyi.school.mapper.SchoolMapper;
import com.ruoyi.system.mapper.SysUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 申请列表Service业务层处理
 *
 * @author ruoyi
 * @date 2024-10-17
 */
@Service
public class ApplicationServiceImpl extends ServiceImpl<ApplicationMapper, Application>
        implements IApplicationService {
    private static final String REDIS_KEY = "application_times";
    @Autowired
    private ApplicationMapper applicationMapper;
    @Autowired
    private IMessageService messageService;
    @Autowired
    private SchoolMapper schoolMapper;
    @Autowired
    private SysUserMapper sysUserMapper;

    /**
     * 查询申请列表
     *
     * @param id 申请列表主键
     * @return 申请列表
     */
    @Override
    public Application selectApplicationById(Long id) {
        return applicationMapper.selectApplicationById(id);
    }

    /**
     * 查询申请列表列表
     *
     * @param application 申请列表
     * @return 申请列表
     */
    @Override
    public List<Application> selectApplicationList(Application application) {
        return applicationMapper.selectApplicationList(application);
    }

    /**
     * 新增申请列表
     *
     * @param application 申请列表
     * @return 结果
     */
    @Override
    public int insertApplication(HttpServletRequest request, Application application) {
        return addApplication(application);
    }

    /**
     * 修改申请列表
     *
     * @param application 申请列表
     * @return 结果
     */
    @Override
    public int updateApplication(Application application) {
        application.setUpdateTime(DateUtils.getNowDate());
        return applicationMapper.updateApplication(application);
    }

    /**
     * 批量删除申请列表
     *
     * @param ids 需要删除的申请列表主键
     * @return 结果
     */
    @Override
    public int deleteApplicationByIds(Long[] ids) {
        return applicationMapper.deleteApplicationByIds(ids);
    }

    /**
     * 删除申请列表信息
     *
     * @param id 申请列表主键
     * @return 结果
     */
    @Override
    public int deleteApplicationById(Long id) {
        return applicationMapper.deleteApplicationById(id);
    }

    /**
     * 申请通过
     *
     * @param application
     * @return
     */
    @Override
    public int pass(Application application) {
        application = applicationMapper.selectApplicationById(application.getId());
//        添加新学校
        schoolMapper.insertSchool(Application.convert(application));
//        添加校方管理员账号
        SysUser sysUser = new SysUser();
        sysUser.setPassword(SecurityUtils.encryptPassword("123456"));
        sysUser.setUserName(application.getSchoolAttack());
        sysUser.setRealName(application.getSchoolName());
        sysUser.setNickName(application.getSchoolName());
        sysUser.setPhonenumber(application.getSchoolAttack());
        sysUser.setAvatar(application.getSchoolImage());
        sysUser.setRoleId(3L);
        sysUserMapper.insertUser(sysUser);
//        更新状态
        application.setExamineStatus("1");
        return applicationMapper.updateApplication(application);
    }

    /**
     * 申请拒绝
     *
     * @param application
     * @return
     */
    @Override
    public int refuse(Application application) {
//        暂时保留拒绝原因
        String refuseReason = application.getRefuseReason();
//        获取id对应的申请
        application = applicationMapper.selectApplicationById(application.getId());
//        更新审核状态
        application.setExamineStatus("0");
        application.setRefuseReason(refuseReason);
        return applicationMapper.updateApplication(application);
    }

    /**
     * 查询申请结果
     *
     * @param application
     * @return
     */
    @Override
    public ResultVo getResult(Application application) {
//        查询申请
        Application resultApplication = null;
        List<Application> applications = applicationMapper.selectApplicationList(application);
        if (ObjectUtils.isNotNull(applications) && ObjectUtils.isNotEmpty(applications)) {
            resultApplication = applications.get(0);
            return new ResultVo(resultApplication.getExamineStatus(), resultApplication.getRefuseReason());
        }
        return null;
    }

    /**
     * 插入申请和消息
     *
     * @param application
     * @return
     */
    private int addApplication(Application application) {
        Message message = new Message();
        message.setContent(application.getSchoolName() + "申请入驻！");
        message.setType("enter");
        message.setSendTime(DateUtils.getNowDate());
        application.setCreateTime(DateUtils.getNowDate());
        try {
            applicationMapper.insertApplication(application);
            return messageService.insertMessage(message);
        } catch (Exception e) {
            throw new RuntimeException("不可重复申请");
        }

    }
}
